﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CE.iPhone.PList;
namespace cczEdit
{
    
    public partial class UCHeroData : UserControl
    {
        List<Hero> heros;
        List<Team> teams;
        PListRoot root;
        String cczDirector;

        public UCHeroData()
        {
            InitializeComponent();
        }
        public UCHeroData(String cczDirector_)
        {
            InitializeComponent();
            cczDirector = cczDirector_;
            heros = new List<Hero>();
            teams = new List<Team>();

            
            listView1.Columns.Add("ID", 40, HorizontalAlignment.Left);
            listView1.Columns.Add("姓名", 120, HorizontalAlignment.Left);
            LoadTeamData();
            LoadHeroData();
            teams.Sort(Team.compare);
            foreach (Team team in teams)
            {
                comboBoxTeam.Items.Add(team.name);
            }
            ShowHeros();
        }
        void LoadHeroData()
        {
            root = PListRoot.Load(cczDirector + "\\HeroData.plist");
            PListDict rootDic = (PListDict)root.Root;
            PListDict herosDic = (PListDict)rootDic["Heros"];
            foreach (KeyValuePair<String, IPListElement> a in herosDic)
            {
                PListDict heroDic = (PListDict)a.Value;
                PListInteger num = (PListInteger)heroDic["Num"];
                PListString name = (PListString)heroDic["Name"];
                PListInteger numHead = (PListInteger)heroDic["NumHead"];
                PListInteger numMsgCrit = (PListInteger)heroDic["NumMsgCrit"];
                PListInteger numMsgDeath = (PListInteger)heroDic["NumMsgDeath"];
                PListInteger numRAnim = (PListInteger)heroDic["NumRAnim"];
                PListInteger numSAnim = (PListInteger)heroDic["NumSAnim"];
                PListInteger numTeam = (PListInteger)heroDic["NumTeam"];
                PListString dspt = (PListString)heroDic["Dspt"];
                PListInteger agile = (PListInteger)heroDic["Agile"];
                PListInteger command = (PListInteger)heroDic["Command"];
                PListInteger force = (PListInteger)heroDic["Force"];
                PListInteger intelligence = (PListInteger)heroDic["Intelligence"];
                PListInteger luck = (PListInteger)heroDic["Luck"];
                PListInteger HP = (PListInteger)heroDic["HP"];
                PListInteger MP = (PListInteger)heroDic["MP"];
                Hero hero = new Hero();
                hero.name = name.Value;
                hero.num = num.Value;
                hero.numHead = numHead.Value;
                hero.numMsgCrit = numMsgCrit.Value;
                hero.numMsgDeath = numMsgDeath.Value;
                hero.numRAnim = numRAnim.Value;
                hero.numSAnim = numSAnim.Value;
                hero.numTeam = numTeam.Value;
                hero.dspt = dspt.Value;
                hero.agile = agile.Value;
                hero.command = command.Value;
                hero.force = force.Value;
                hero.intelligence = intelligence.Value;
                hero.luck = luck.Value;
                hero.hp = HP.Value;
                hero.mp = MP.Value;
                heros.Add(hero);

            }
            

        }
        void LoadTeamData()
        {
            root = PListRoot.Load(cczDirector + "\\TeamData.plist");
            PListDict rootDic = (PListDict)root.Root;
            PListDict teamsDic = (PListDict)rootDic["Teams"];
            foreach (KeyValuePair<String, IPListElement> a in teamsDic)
            {
                PListDict teamDic = (PListDict)a.Value;
                PListString name = (PListString)teamDic["Name"];
                PListInteger num = (PListInteger)teamDic["Num"];
                PListString dspt = (PListString)teamDic["Dspt"];
                PListInteger type = (PListInteger)teamDic["Type"];
                PListInteger numAttackRange = (PListInteger)teamDic["NumAttackRange"];
                PListInteger weaponType = (PListInteger)teamDic["WeaponType"];
                PListInteger armorType = (PListInteger)teamDic["ArmorType"];
                PListInteger attack = (PListInteger)teamDic["Attack"];
                PListInteger defend = (PListInteger)teamDic["Defend"];
                PListInteger willpower = (PListInteger)teamDic["Willpower"];
                PListInteger Erupt = (PListInteger)teamDic["Erupt"];
                PListInteger morale = (PListInteger)teamDic["Morale"];
                PListInteger move = (PListInteger)teamDic["Move"];
                PListInteger HPDelta = (PListInteger)teamDic["HPDelta"];
                PListInteger MPDelta = (PListInteger)teamDic["MPDelta"];
                PListDict learnMagicLevel = (PListDict)teamDic["LearnMagicLevel"];

                Team team = new Team();
                team.name = name.Value;
                team.num = num.Value;
                team.dspt = dspt.Value;
                team.type = type.Value;
                team.numAttackRange = numAttackRange.Value;
                team.weaponType = weaponType;
                team.armorType = armorType;
                team.attack = attack;
                team.defend = defend;
                team.willpower = willpower;
                team.erupt = Erupt;
                team.morale = morale;
                team.move = move;
                team.HPDelta = HPDelta;
                team.MPDelta = MPDelta;

                foreach (KeyValuePair<String, IPListElement> l in learnMagicLevel)
                {
                    PListInteger value = (PListInteger)l.Value;
                    team.learnMagicLevel.Add(Convert.ToInt64(l.Key), value.Value);
                }
                teams.Add(team);

            }
        }
        void ShowHeros()
        {
            listView1.Items.Clear();
            foreach (Hero hero in heros)
            {
                ListViewItem item = new ListViewItem(new String[] { "" + hero.num, hero.name });
                listView1.Items.Add(item);
            }
            listView1.Refresh();
            if (listView1.Items.Count != 0)
            {
                listView1.Items[listView1.Items.Count-1].Selected = true;
            }
        }
        public void SaveAllData()
        {
            if (listView1.SelectedItems.Count == 0)
            {
                return;
            }
            ListViewItem item = listView1.SelectedItems[0];
            SaveDetail(Convert.ToInt32(item.SubItems[0].Text));

            root.Save(cczDirector + "\\HeroData.plist");

        }
        void ShowDetail(int index)
        {
            Hero hero = heros.Find((h) => { return h.num == index; });
            textBoxName.Text = hero.name;
            numericUpDownHead.Value = hero.numHead;
            numericUpDownRDrama.Value = hero.numRAnim;
            numericUpDownSDrama.Value = hero.numSAnim;
            numericUpDownCrit.Value = hero.numMsgCrit;
            numericUpDownExit.Value = hero.numMsgDeath;
            textBoxForce.Text = hero.force.ToString();
            textBoxIntelligence.Text = hero.intelligence.ToString();
            textBoxCommand.Text = hero.command.ToString();
            textBoxAgile.Text = hero.agile.ToString();
            textBoxLuck.Text = hero.luck.ToString();
            textBoxHPInit.Text = hero.hp.ToString();
            textBoxMPInit.Text = hero.mp.ToString();
            Team team = teams.Find((t) => { return t.num == hero.numTeam; });
            comboBoxTeam.Text = team.name;
            textBoxDspt.Text = hero.dspt;
            pictureBoxSDrama.Image = Image.FromFile(cczDirector + "\\UnitSPC\\UnitSPC" + numericUpDownSDrama.Value + ".png");
            pictureBoxRDrama.Image = Image.FromFile(cczDirector + "\\PMapObj\\PMapObj" + numericUpDownRDrama.Value + ".png");
            pictureBoxHead.Image = Image.FromFile(cczDirector + "\\Face\\Face" + numericUpDownHead.Value + ".png");
        }

        void SaveDetail(int index)
        {
           Hero hero = heros.Find((h) => { return h.num == index; });
           hero.name= textBoxName.Text ;
           hero.numHead = Convert.ToInt64(numericUpDownHead.Value);
           hero.numRAnim = Convert.ToInt64(numericUpDownRDrama.Value);
           hero.numSAnim = Convert.ToInt64(numericUpDownSDrama.Value);
           hero.numMsgCrit = Convert.ToInt64(numericUpDownCrit.Value);
           hero.numMsgDeath =Convert.ToInt64( numericUpDownExit.Value);
           hero.force =Convert.ToInt64( textBoxForce.Text);
           hero.intelligence= Convert.ToInt64(textBoxIntelligence.Text) ;
           hero.command= Convert.ToInt64(textBoxCommand.Text) ;
          hero.agile=Convert.ToInt64( textBoxAgile.Text) ;
           hero.luck=Convert.ToInt64(textBoxLuck.Text);
            hero.hp =Convert.ToInt64(textBoxHPInit.Text );
            hero.mp = Convert.ToInt64(textBoxMPInit.Text);
            hero.dspt = textBoxDspt.Text;
           Team team = teams.Find((t) => { return t.name == comboBoxTeam.Text; });
           hero.numTeam = team.num;
           PListDict rootDic = (PListDict)root.Root;
           PListDict herosDic = (PListDict)rootDic["Heros"];
           foreach (KeyValuePair<String, IPListElement> a in herosDic)
           {
               PListDict heroDic = (PListDict)a.Value;
               PListInteger num = (PListInteger)heroDic["Num"];
               if (num.Value == hero.num)
               {
                   heroDic["Name"] = new PListString(hero.name);
                   heroDic["NumHead"] = new PListInteger(hero.numHead);
                   heroDic["NumRAnim"] = new PListInteger(hero.numRAnim);
                   heroDic["NumSAnim"] = new PListInteger(hero.numSAnim);
                   heroDic["NumMsgCrit"] = new PListInteger(hero.numMsgCrit);
                   heroDic["NumMsgDeath"] = new PListInteger(hero.numMsgDeath);
                   heroDic["Force"] = new PListInteger(hero.force);
                   heroDic["Agile"] = new PListInteger(hero.agile);
                   heroDic["Command"] = new PListInteger(hero.command);
                   heroDic["Intelligence"] = new PListInteger(hero.intelligence);
                   heroDic["Luck"] = new PListInteger(hero.luck);
                   heroDic["HP"] = new PListInteger(hero.hp);
                   heroDic["MP"] = new PListInteger(hero.mp);
                   heroDic["Dspt"] = new PListString(hero.dspt);
                   heroDic["NumTeam"] = new PListInteger(hero.numTeam);
               }
           }

        }
        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0) {
                return;
            }
            ListViewItem item = listView1.SelectedItems[0];
            ShowDetail(Convert.ToInt32(item.SubItems[0].Text));
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            //PListDict rootDic = (PListDict)root.Root;
            //PListDict herosDic = (PListDict)rootDic["Heros"];
            //foreach (KeyValuePair<String, IPListElement> a in herosDic)
            //{
            //    PListDict heroDic = (PListDict)a.Value;
            //    PListInteger num = (PListInteger)heroDic["Num"];
            //    if (num.Value >= 319)
            //    {
            //        heroDic["Num"] = new PListInteger(num.Value - 1);
            //    }
            //}
            if (listView1.SelectedItems.Count == 0)
            {
                return;
            }
            ListViewItem item = listView1.SelectedItems[0];
            SaveDetail(Convert.ToInt32(item.SubItems[0].Text));
            listView1.Refresh();
        }

        private void buttonRecover_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
            {
                return;
            }
            ListViewItem item = listView1.SelectedItems[0];
            ShowDetail(Convert.ToInt32(item.SubItems[0].Text));
        }

        private void numericUpDownHead_ValueChanged(object sender, EventArgs e)
        {
            pictureBoxHead.Image = Image.FromFile(cczDirector + "\\Face\\Face" + numericUpDownHead.Value + ".png");

        }

        private void numericUpDownRDrama_ValueChanged(object sender, EventArgs e)
        {
            pictureBoxRDrama.Image = Image.FromFile(cczDirector + "\\PMapObj\\PMapObj" + numericUpDownRDrama.Value + ".png");

        }

        private void numericUpDownSDrama_ValueChanged(object sender, EventArgs e)
        {
            pictureBoxSDrama.Image = Image.FromFile(cczDirector + "\\UnitSPC\\UnitSPC" + numericUpDownSDrama.Value + ".png");

        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
                PListDict rootDic = (PListDict)root.Root;
                PListDict herosDic = (PListDict)rootDic["Heros"];
                PListInteger newNum = (PListInteger)rootDic["NewNum"];
                rootDic["NewNum"] = new PListInteger(newNum + 1);

                Hero hero = new Hero();
                hero.num = newNum;
                hero.name = textBoxName.Text;
                hero.numHead = Convert.ToInt64(numericUpDownHead.Value);
                hero.numRAnim = Convert.ToInt64(numericUpDownRDrama.Value);
                hero.numSAnim = Convert.ToInt64(numericUpDownSDrama.Value);
                hero.numMsgCrit = Convert.ToInt64(numericUpDownCrit.Value);
                hero.numMsgDeath = Convert.ToInt64(numericUpDownExit.Value);
                hero.force = Convert.ToInt64(textBoxForce.Text);
                hero.intelligence = Convert.ToInt64(textBoxIntelligence.Text);
                hero.command = Convert.ToInt64(textBoxCommand.Text);
                hero.agile = Convert.ToInt64(textBoxAgile.Text);
                hero.luck = Convert.ToInt64(textBoxLuck.Text);
                hero.hp = Convert.ToInt64(textBoxHPInit.Text);
                hero.mp = Convert.ToInt64(textBoxMPInit.Text);
                hero.dspt = textBoxDspt.Text;
                Team team = teams.Find((t) => { return t.name == comboBoxTeam.Text; });
                hero.numTeam = team.num;


                PListDict heroDic = new PListDict();
                heroDic["Num"] = new PListInteger(hero.num);
                heroDic["Name"] = new PListString(hero.name);
                heroDic["NumHead"] = new PListInteger(hero.numHead);
                heroDic["NumRAnim"] = new PListInteger(hero.numRAnim);
                heroDic["NumSAnim"] = new PListInteger(hero.numSAnim);
                heroDic["NumMsgCrit"] = new PListInteger(hero.numMsgCrit);
                heroDic["NumMsgDeath"] = new PListInteger(hero.numMsgDeath);
                heroDic["Force"] = new PListInteger(hero.force);
                heroDic["Agile"] = new PListInteger(hero.agile);
                heroDic["Command"] = new PListInteger(hero.command);
                heroDic["Intelligence"] = new PListInteger(hero.intelligence);
                heroDic["Luck"] = new PListInteger(hero.luck);
                heroDic["HP"] = new PListInteger(hero.hp);
                heroDic["MP"] = new PListInteger(hero.mp);
                heroDic["Dspt"] = new PListString(hero.dspt);
                heroDic["NumTeam"] = new PListInteger(hero.numTeam);
                heros.Add(hero);
                herosDic.Add("Hero" + newNum, heroDic);
            ShowHeros();
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
            {
                return;
            }
            ListViewItem item = listView1.SelectedItems[0];
            heros.RemoveAll((m) => { return m.num == (Convert.ToInt32(item.SubItems[0].Text)); });
            PListDict rootDic = (PListDict)root.Root;
            PListDict herosDic = (PListDict)rootDic["Heros"];
            String heroKey = null;
            foreach (KeyValuePair<String, IPListElement> a in herosDic)
            {
                PListDict heroDic = (PListDict)a.Value;
                PListInteger num = (PListInteger)heroDic["Num"];
                if (num.Value == Convert.ToInt32(item.SubItems[0].Text))
                {
                    heroKey = a.Key;
                }
            }
            if (heroKey != null)
                herosDic.Remove(heroKey);
            ShowHeros();

        }

        private void buttonMutiAdd_Click(object sender, EventArgs e)
        {
            for (int i = 1; i <= numericUpDownBatSum.Value; ++i)
            {
                PListDict rootDic = (PListDict)root.Root;
                PListDict herosDic = (PListDict)rootDic["Heros"];
                PListInteger newNum = (PListInteger)rootDic["NewNum"];
                rootDic["NewNum"] = new PListInteger(newNum + 1);

                Hero hero = new Hero();
                hero.num = newNum;
                hero.name = textBoxBatPre.Text + i;
                hero.numHead = Convert.ToInt64(numericUpDownHead.Value);
                hero.numRAnim = Convert.ToInt64(numericUpDownRDrama.Value);
                hero.numSAnim = Convert.ToInt64(numericUpDownSDrama.Value);
                hero.numMsgCrit = Convert.ToInt64(numericUpDownCrit.Value);
                hero.numMsgDeath = Convert.ToInt64(numericUpDownExit.Value);
                hero.force = Convert.ToInt64(textBoxForce.Text);
                hero.intelligence = Convert.ToInt64(textBoxIntelligence.Text);
                hero.command = Convert.ToInt64(textBoxCommand.Text);
                hero.agile = Convert.ToInt64(textBoxAgile.Text);
                hero.luck = Convert.ToInt64(textBoxLuck.Text);
                hero.hp = Convert.ToInt64(textBoxHPInit.Text);
                hero.mp = Convert.ToInt64(textBoxMPInit.Text);
                hero.dspt = textBoxDspt.Text;
                Team team = teams.Find((t) => { return t.name == comboBoxTeam.Text; });
                hero.numTeam = team.num;


                PListDict heroDic = new PListDict();
                heroDic["Num"] = new PListInteger(hero.num);
                heroDic["Name"] = new PListString(hero.name);
                heroDic["NumHead"] = new PListInteger(hero.numHead);
                heroDic["NumRAnim"] = new PListInteger(hero.numRAnim);
                heroDic["NumSAnim"] = new PListInteger(hero.numSAnim);
                heroDic["NumMsgCrit"] = new PListInteger(hero.numMsgCrit);
                heroDic["NumMsgDeath"] = new PListInteger(hero.numMsgDeath);
                heroDic["Force"] = new PListInteger(hero.force);
                heroDic["Agile"] = new PListInteger(hero.agile);
                heroDic["Command"] = new PListInteger(hero.command);
                heroDic["Intelligence"] = new PListInteger(hero.intelligence);
                heroDic["Luck"] = new PListInteger(hero.luck);
                heroDic["HP"] = new PListInteger(hero.hp);
                heroDic["MP"] = new PListInteger(hero.mp);
                heroDic["Dspt"] = new PListString(hero.dspt);
                heroDic["NumTeam"] = new PListInteger(hero.numTeam);
                heros.Add(hero);
                herosDic.Add("Hero" + newNum, heroDic);
            }


            ShowHeros();
        }
        

    }
    public class Hero
    {
        public long agile;
        public long command;
        public long force;
        public long hp;
        public long intelligence;
        public long luck;
        public long mp;
        public String name;
        public long num;
        public long numHead;
        public long numMsgCrit;
        public long numMsgDeath;
        public long numRAnim;
        public long numSAnim;
        public long numTeam;
        public String dspt; 
    }
    public class HeroNameCompare : IComparer<Hero>     //姓名升序
    {
        public int Compare(Hero x, Hero y)
        {
            return x.name.CompareTo(y.name);
        }
    }
    public class HeroNumCompare : IComparer<Hero>     //姓名升序
    {
        public int Compare(Hero x, Hero y)
        {
            return x.num.CompareTo(y.num);
        }
    }
}
